﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ClientApp
{
    public partial class CandidateForm : Form
    {
        private ClientApp _parent;

        public CandidateForm(ClientApp parent)
        {
            _parent = parent;
            
            InitializeComponent();
        }

        private void ResetFilterButton_Click(object sender, EventArgs e)
        {
            ActivityFilterComboBox.ResetText();

            for (int i = 0; i < SkillFilterListBox.Items.Count; i++)
            {
                SkillFilterListBox.SetItemChecked(i, false);
            }

            SalaryFilterMaskedTextBox.ResetText();
        }

        private void FindVacanciesButton_Click(object sender, EventArgs e)
        {
            if (ActivityFilterComboBox.SelectedIndex < 0 ||
                SalaryFilterMaskedTextBox.Text == "")
            {
                MessageBox.Show("Заполните все текстовые поля формы.", "Ошибка");
                return;
            }

            FindVacancyDataGridView.Rows.Clear();

            List<Skill> checked_skills = (from object item in SkillFilterListBox.CheckedItems
                                          select DatabaseAccess.Instance.SkillByTitle(item as string)).ToList();

            List<Vacancy> query_result = DatabaseAccess.Instance.Vacancies(
                DatabaseAccess.Instance.ActivityByTitle(ActivityFilterComboBox.Text),
                checked_skills,
                Convert.ToUInt32(SalaryFilterMaskedTextBox.Text));
            
            foreach (Vacancy res_item in query_result)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.AddRange(new DataGridViewCell[4] {new DataGridViewTextBoxCell(), 
                                                            new DataGridViewTextBoxCell(), 
                                                            new DataGridViewTextBoxCell(), 
                                                            new DataGridViewTextBoxCell()});
                row.Cells[0].Value = res_item.Id;
                row.Cells[1].Value = res_item.Title;
                row.Cells[2].Value = res_item.Salary;
                row.Cells[3].Value = res_item.Employer.Name;

                FindVacancyDataGridView.Rows.Add(row);
            }

            FindVacancyResultGroupBox.Text = "Найдено: " + FindVacancyDataGridView.Rows.Count.ToString();
            ApplyForVacancyButton.Enabled = FindVacancyDataGridView.Rows.Count > 0;
        }

        private void ApplyForVacancyButton_Click(object sender, EventArgs e)
        {
            if (!DatabaseAccess.Instance.CandidateExists(LoginTextBox.Text))
            {
                MessageBox.Show("Логин не существует в системе. Проверьте правильность ввода или зарегистрируйтесь.",
                                "Error");
                return;
            }

            _parent.CandidateRequests.Add(
                "Соискатель \"" + LoginTextBox.Text + 
                "\" заинтересован в вакансии " + (uint)FindVacancyDataGridView.SelectedRows[0].Cells[0].Value);

            MessageBox.Show("Заявка отправлена.");
        }

        private void CandidateForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _parent.CandidateFormOpened = false;
        }

        private void CandidateForm_Load(object sender, EventArgs e)
        {
            FindVacancyDataGridView.Columns.Add("Id", "id");
            FindVacancyDataGridView.Columns["Id"].Visible = false;
            FindVacancyDataGridView.Columns.Add("Title", "Вакансия");
            FindVacancyDataGridView.Columns.Add("Salary", "Оклад");
            FindVacancyDataGridView.Columns.Add("EmployerName", "Работодатель");

            List<Activity> activities = DatabaseAccess.Instance.Activities();

            foreach (Activity activity in activities)
            {
                ActivityFilterComboBox.Items.Add(activity.Title);
            }

            List<Skill> skills = DatabaseAccess.Instance.Skills();
            foreach (Skill skill in skills)
            {
                SkillFilterListBox.Items.Add(skill.Title);
            }
            
        }
    }
}
